﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Threading;
using System.Globalization;
using Nivdal.AuxCode;

namespace Nivdal
{
    public partial class AdministerPunishments : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                Response.Redirect("~/Default.aspx");
            GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            
            if (ou.UserAccessLevel == ("1") || ou.UserAccessLevel == ("3"))
            {
                Response.Redirect("~/Default.aspx");
            }
            string UserCountry = (GND.ReadNivdalIntData("@UserId", "Nivdal_GetCountryByUser", ou.UserId));
            if ((!IsPostBack) && ( ou.UserAccessLevel != "5"))
            {
                this.InappropriateBehaviourReportsSqlDataSource.SelectParameters.Add("Country", UserCountry);
            }
            this.Title = Resources.Resource.Default_PageTitle + ", " + Resources.Resource.AdministerPunishments_PageTitle;
        }

        protected void InappropriateBehaviourReportsGridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Visible = false;
        }

        protected void SubmitAcation(string ReportId, string ActionId)
        {
            //GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
            OnlineUser ou = (OnlineUser)Session["User"];
            string Reviewer = ou.UserId;

            string conn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            System.Data.IDbConnection dbConnection = new System.Data.SqlClient.SqlConnection(conn);
            System.Data.IDbCommand dbCommand = new System.Data.SqlClient.SqlCommand();
            dbCommand.CommandText = "Nivdal_SetInappropreateBehaviourReportAction";
            dbCommand.CommandType = CommandType.StoredProcedure;
            dbCommand.Connection = dbConnection;

            SqlParameter dbParam_Reviewer = new SqlParameter();
            dbParam_Reviewer.ParameterName = "@Reviewer";
            dbParam_Reviewer.Value = Reviewer;
            dbParam_Reviewer.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_Reviewer);

            SqlParameter dbParam_ReportId = new SqlParameter();
            dbParam_ReportId.ParameterName = "@ReportId";
            dbParam_ReportId.Value = ReportId;
            dbParam_ReportId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ReportId);

            SqlParameter dbParam_ActionId = new SqlParameter();
            dbParam_ActionId.ParameterName = "@ActionId";
            dbParam_ActionId.Value = ActionId;
            dbParam_ActionId.DbType = DbType.String;
            dbCommand.Parameters.Add(dbParam_ActionId);

            SqlParameter dbParam_Language = new SqlParameter();
            dbParam_Language.ParameterName = "@Language";
            dbParam_Language.Value = ou.Language;
            dbParam_Language.DbType = DbType.Decimal;
            dbCommand.Parameters.Add(dbParam_Language);

            SqlParameter dbParam_Answer = new SqlParameter();
            dbParam_Answer.ParameterName = "@Answer";
            dbParam_Answer.Value = "";
            dbParam_Answer.DbType = DbType.String;
            dbParam_Answer.Direction = ParameterDirection.Output;
            dbParam_Answer.Size = 64;
            dbCommand.Parameters.Add(dbParam_Answer);

            try
            {

                dbConnection.Open();
                dbCommand.ExecuteScalar();
                MessageLabel.Text = dbParam_Answer.Value.ToString();
                InappropriateBehaviourReportsGridView.DataBind();
            }
            catch (Exception SQLDBException)
            {
                this.MessageLabel.Text = Convert.ToString(SQLDBException);
            }

            finally
            {
                dbConnection.Close();
            }

        }



        protected void AdminAction(Object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName == "Submit")
            {
                // get the row index stored in the CommandArgument property
                int index = Convert.ToInt32(e.CommandArgument);
                // get the GridViewRow where the command is raised
                GridViewRow row = InappropriateBehaviourReportsGridView.Rows[index];
                string ReportId = row.Cells[0].Text;
                DropDownList ActionDropDownList = (DropDownList)row.FindControl("ActionDropDownList");
                string ActionId = ActionDropDownList.SelectedItem.Value;
                //this.Message.Text = row.Cells[0].Text.ToString();
                //this.MessageLabel.Text = ActionValue;
                SubmitAcation(ReportId, ActionId);

            }
        }

        protected void TreatMailReport(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Submit")
            {
                GetNivdalData.GetNivdalData GND = new GetNivdalData.GetNivdalData();
                OnlineUser ou = (OnlineUser)Session["User"];
                GridViewRow row = MailReportedSpamGridview.Rows[Convert.ToInt32(e.CommandArgument)];
                string GMNote = (row.FindControl("ReportNoteText") as CustomWebControls.TextArea).Text.Trim();
                string MarkAsSpam = (row.FindControl("MarkReportAsSpamCB") as CheckBox).Checked ? "1" : "0" ;
                string CreateReport = (row.FindControl("CreateReportCB") as CheckBox).Checked ? "1" : "0";
                string MailId = (row.FindControl("MailIdHiddenField") as HiddenField).Value;

                GND.ExecNivdalSQL("MailId", "GMUserId", "GMNote", "MarkAsSpam", "CreateWarning", "Nivdal_TreatSpamReportByGM", MailId, ou.UserId, GMNote, MarkAsSpam, CreateReport);

                MailReportedSpamGridview.DataBind();

            }

        }

        protected override void InitializeCulture()
        {
            OnlineUser ou = (OnlineUser)Session["User"];
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(ou.UICulture);
            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ou.Culture);
        }

        protected string SubmitActionLinkButtonOnClientClick()
        {
            return ("return confirm('" + Resources.Resource.AdministerPunishments_SubmitActionLinkButton + "')");
        }

    }
}